using MicroMsg.Manager;
using MicroMsg.Plugin;
using System;
using System.Collections.Generic;
using System.Text;
namespace MicroMsg.Storage
{
	public class MsgVoiceStorage : MsgTransStorageProxy
	{
		public static string buildVoiceFilePath(ChatMsg chatMsg)
		{
			if (chatMsg == null)
			{
				return null;
			}
			if (chatMsg.nMsgType != 34)
			{
				return null;
			}
			string pluginNameByChatMsg = ExtentCenter.getPluginNameByChatMsg(chatMsg);
			if (!string.IsNullOrEmpty(pluginNameByChatMsg))
			{
				if (chatMsg.nIsSender == 1)
				{
					return MsgVoiceStorage.getUpVoiceFileNameForPlugin(pluginNameByChatMsg, chatMsg.strClientMsgId);
				}
				return MsgVoiceStorage.getDownVoiceFileNameForPlugin(pluginNameByChatMsg, chatMsg.nMsgSvrID);
			}
			else
			{
				if (chatMsg.nIsSender == 1)
				{
					return MsgVoiceStorage.getUploadVoiceFilePath(chatMsg.strClientMsgId);
				}
				return MsgVoiceStorage.getDownloadVoiceFilePath(chatMsg.nMsgSvrID);
			}
		}
		public static string getDownVoiceFileNameForPlugin(string pluginName, int nMsgSvrID)
		{
			return string.Concat(new object[]
			{
				StorageIO.getPluginPath(pluginName),
				"/voice/dlvoice_",
				nMsgSvrID,
				".amr"
			});
		}
		public static string getUpVoiceFileNameForPlugin(string pluginName, string clientMsgID)
		{
			return string.Concat(new object[]
			{
				StorageIO.getPluginPath(pluginName),
				"/voice/upvoice_",
				(uint)clientMsgID.GetHashCode(),
				".amr"
			});
		}
		public static string getDownloadVoiceFilePath(int nMsgSvrID)
		{
			return string.Concat(new object[]
			{
				StorageIO.getVoicePath(),
				"/downloadvoice_",
				nMsgSvrID,
				".amr"
			});
		}
		public static string getUploadVoiceFilePath(string clientMsgID)
		{
			return string.Concat(new object[]
			{
				StorageIO.getVoicePath(),
				"/upvoice_",
				(uint)clientMsgID.GetHashCode(),
				".amr"
			});
		}
		public bool updateTransByClientMsgId(MsgTrans msgTrans_0)
		{
			return base.update(msgTrans_0, (MsgTrans msgTrans_1) => msgTrans_1.strClientMsgId == msgTrans_0.strClientMsgId);
		}
		public bool saveVoiceBlockData(MsgTrans trans, byte[] buffer, ChatMsg chatMsg_0 = null)
		{
			string text;
			int num;
			if (trans.nTransType == 3)
			{
				text = MsgVoiceStorage.buildVoiceFilePath(chatMsg_0);
				num = trans.nTotalDataLen;
			}
			else
			{
				if (trans.nTransType != 4 || trans.nMsgSvrID == 0)
				{
					return false;
				}
				chatMsg_0 = StorageMgr.chatMsg.getBySvrID(ChatMsgHelper.getTalker(trans.strToUserName, trans.strFromUserName), trans.nMsgSvrID);
				text = MsgVoiceStorage.buildVoiceFilePath(chatMsg_0);
				num = trans.nTransDataLen;
			}
			if (string.IsNullOrEmpty(text))
			{
				return false;
			}
			UTF8Encoding uTF8Encoding = new UTF8Encoding();
			byte[] bytes = uTF8Encoding.GetBytes("#!AMR\n");
			if (num == 0)
			{
				byte[] array = new byte[buffer.Length + bytes.Length];
				Buffer.BlockCopy(bytes, 0, array, 0, bytes.Length);
				Buffer.BlockCopy(buffer, 0, array, bytes.Length, buffer.Length);
				StorageIO.writeToFile(text, 0, new byte[][]
				{
					array
				});
				chatMsg_0.strPath = text;
				StorageMgr.chatMsg.modifyMsg(chatMsg_0);
			}
			else
			{
				StorageIO.writeToFile(text, num + bytes.Length, new byte[][]
				{
					buffer
				});
			}
			return true;
		}
		public byte[] loadVoiceFileData(MsgTrans trans)
		{
			string text;
			if (trans.nTransType == 3)
			{
				ChatMsg msg = StorageMgr.chatMsg.getMsg(trans.nMsgLocalID);
				text = MsgVoiceStorage.buildVoiceFilePath(msg);
			}
			else
			{
				if (trans.nTransType != 4 || trans.nMsgSvrID == 0)
				{
					return null;
				}
				ChatMsg bySvrID = StorageMgr.chatMsg.getBySvrID(ChatMsgHelper.getTalker(trans.strToUserName, trans.strFromUserName), trans.nMsgSvrID);
				text = MsgVoiceStorage.buildVoiceFilePath(bySvrID);
			}
			if (string.IsNullOrEmpty(text))
			{
				return null;
			}
			UTF8Encoding uTF8Encoding = new UTF8Encoding();
			byte[] bytes = uTF8Encoding.GetBytes("#!AMR\n");
			if (trans.nTotalDataLen <= 0)
			{
				return null;
			}
			byte[] array = StorageIO.readFromFile(text, bytes.Length, trans.nTotalDataLen);
			if (array != null)
			{
				if (array.Length == trans.nTotalDataLen)
				{
					return array;
				}
			}
			return null;
		}
		public List<MsgTrans> getCleanUploadVoiceList(int number, long time)
		{
			return base.getList(0, number, (MsgTrans msgTrans_0) => msgTrans_0.nTransType == 3 && (msgTrans_0.nTotalDataLen <= 0 || msgTrans_0.nStatus == 5 || msgTrans_0.nTotalDataLen != msgTrans_0.nRecordLength || msgTrans_0.nEndFlag != 1));
		}
		public List<MsgTrans> getResumeUploadVoiceList(int number, long time)
		{
			return base.getList(0, number, (MsgTrans msgTrans_0) => msgTrans_0.nTransType == 3 && msgTrans_0.nTotalDataLen > 0 && msgTrans_0.nStatus != 5 && msgTrans_0.nTotalDataLen == msgTrans_0.nRecordLength && msgTrans_0.nEndFlag == 1);
		}
		public bool delPendingDownloadVoice()
		{
			return base.method_3(delegate(MsgTrans msgTrans_0)
			{
				if (msgTrans_0.nTransType == 4)
				{
					if (msgTrans_0.nStatus == 4)
					{
						return true;
					}
				}
				return msgTrans_0.nStatus == 5;
			});
		}
		public List<MsgTrans> getPendingDownLoadVoiceList()
		{
			return base.getList((MsgTrans msgTrans_0) => msgTrans_0.nTransType == 4 && msgTrans_0.nStatus != 4 && msgTrans_0.nStatus != 5);
		}
	}
}
